package com.ruoyi.park.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.park.domain.BuiGreenArea;
import com.ruoyi.park.mapper.BuiGreenAreaMapper;
import com.ruoyi.park.service.IBuiGreenAreaService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 绿化区域Service业务层处理
 *
 * @author cjie
 * @date 2021-07-20
 */
@Service
@RequiredArgsConstructor
public class BuiGreenAreaServiceImpl extends LinkFileServiceImpl implements IBuiGreenAreaService {
    private final BuiGreenAreaMapper buiGreenAreaMapper;

    /**
     * 查询绿化区域
     *
     * @param id 绿化区域ID
     * @return 绿化区域
     */
    @Override
    public BuiGreenArea selectBuiGreenAreaById(String id) {
        BuiGreenArea buiGreenArea = buiGreenAreaMapper.selectBuiGreenAreaById(id);
        buiGreenArea.setFileList(getFileList(id));
        return buiGreenArea;
    }

    /**
     * 查询绿化区域列表
     *
     * @param buiGreenArea 绿化区域
     * @return 绿化区域
     */
    @Override
    public List<BuiGreenArea> selectBuiGreenAreaList(BuiGreenArea buiGreenArea) {
        return buiGreenAreaMapper.selectBuiGreenAreaList(buiGreenArea);
    }

    /**
     * 新增绿化区域
     *
     * @param buiGreenArea 绿化区域
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertBuiGreenArea(BuiGreenArea buiGreenArea) {

        if(UserConstants.NOT_UNIQUE.equals(checkAreaNumUnique(buiGreenArea))){
            return AjaxResult.error("当前区域编号已存在");
        }

        if (StringUtils.isBlank(buiGreenArea.getId())){
            buiGreenArea.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        }
        buiGreenArea.setCreateTime(DateUtils.getNowDate());
        buiGreenArea.setCreateBy(SecurityUtils.getUsername());

        linkFile(buiGreenArea.getFileIdList(), buiGreenArea.getId());
        return buiGreenAreaMapper.insertBuiGreenArea(buiGreenArea)  > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 校验区域编号是否唯一
     *
     * @param buiGreenArea 区域信息
     * @return 结果
     */
    @Override
    public String checkAreaNumUnique(BuiGreenArea buiGreenArea) {
        BuiGreenArea sourceGreenArea = buiGreenAreaMapper.selectBuiGreenAreaByNum(buiGreenArea.getAreaNum());

        if(sourceGreenArea != null && !sourceGreenArea.getId().equals(buiGreenArea.getId())){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 修改绿化区域
     *
     * @param buiGreenArea 绿化区域
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateBuiGreenArea(BuiGreenArea buiGreenArea) {

        if(UserConstants.NOT_UNIQUE.equals(checkAreaNumUnique(buiGreenArea))){
            return AjaxResult.error("当前区域编号已存在");
        }
        buiGreenArea.setUpdateTime(DateUtils.getNowDate());
        buiGreenArea.setUpdateBy(SecurityUtils.getUsername());

        linkFile(buiGreenArea.getFileIdList(), buiGreenArea.getId());
        return buiGreenAreaMapper.updateBuiGreenArea(buiGreenArea) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 批量删除绿化区域
     *
     * @param ids 需要删除的绿化区域ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBuiGreenAreaByIds(String[] ids) {
        deleteLinkFile(ids);
        return buiGreenAreaMapper.deleteBuiGreenAreaByIds(ids);
    }

    /**
     * 删除绿化区域信息
     *
     * @param id 绿化区域ID
     * @return 结果
     */
    @Override
    public int deleteBuiGreenAreaById(String id) {
        return buiGreenAreaMapper.deleteBuiGreenAreaById(id);
    }
}
